/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System.Data;
using VMukti.Common;
using VMukti.DataAccess;
using System.Data.SqlClient;
using System.Collections.Generic;
using VMukti.Business.CommonDataContracts;
using System.Net;
using System;
using VMuktiAPI;
using VMuktiService;
using VMukti;



namespace VMukti.Business
{
    public class ClsBuddy : ClsBaseObject
    {
        #region Fields

        private int _ID = VMukti.Common.ClsConstants.NullInt;
        private int _MyUserID = VMukti.Common.ClsConstants.NullInt;
        private int _UserID = VMukti.Common.ClsConstants.NullInt;
        private string _DisplayName = VMukti.Common.ClsConstants.NullString;

        private string _MeshID = VMukti.Common.ClsConstants.NullString;
        private string _Status = VMukti.Common.ClsConstants.NullString;

        #endregion 

        #region Properties

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public int MyUserID
        {
            get { return _MyUserID; }
            set 
            { 
                _MyUserID = value;
                _MeshID = "VMukti" + _MyUserID.ToString();
            }
        }

        public int UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }

        public string MeshID
        {
            get { return _MeshID; }
        }

        public string DisplayName
        {
            get { return _DisplayName; }
            set { _DisplayName = value; }
        }
        
        public string Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        #endregion 

        #region Methods

        public override bool MapData(DataRow row)
        {
            ID = GetInt(row, "ID");
            MyUserID = GetInt(row, "MyUserID");
            UserID = GetInt(row, "UserID");
            DisplayName = GetString(row, "DisplayName");

            return base.MapData(row);
        }

        public static ClsBuddy GetByBuddyID(int ID)
        {
            ClsBuddy obj = new ClsBuddy();
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {                
                List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                objInfo.Direction = "Input";
                objInfo.PName = "@pID";
                objInfo.PValue = ID;                
                objInfo.PDBType = "Int";
                objInfo.PSize = 200;
                lstSP.Add(objInfo);

                clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                CSqlInfo.objParam = lstSP;

                //11-Feb-2008
                //shilpa code 
                //super node checking
                DataSet ds;
                try
                {
                    ds = VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGMyBuddy", CSqlInfo).dsInfo;
                    
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    //dataBaseChannelReOpen();
                    e.Data.Add("My Key", "GetByBuddyID(int ID)--:--ClsBuddy.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    ds = VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGMyBuddy", CSqlInfo).dsInfo;
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    //dataBaseChannelReOpen();
                    e.Data.Add("My Key", "GetByBuddyID(int ID)--:--ClsBuddy.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    ds = VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGMyBuddy", CSqlInfo).dsInfo;  
                }
               
                //11-Feb-2008
                //end shilpa code
                if (!obj.MapData(ds.Tables[0])) obj = null;                
            }
            else
            {
                DataSet ds = new VMukti.DataAccess.ClsBuddyDataService().Buddy_GetByID(ID);
                if (!obj.MapData(ds.Tables[0])) obj = null;
            }
            return obj;
        }

        //11-Feb-2008
        //shilpa code
        /*public static void dataBaseChannelReOpen()
        {
            //continue trying untill the OpenDataBaseClient() will open
            bool DataBaseClientStatus = false;
            bool endPoint = false;
            while (!DataBaseClientStatus)
            {
                DataBaseClientStatus = Business.clsDataBaseChannel.OpenDataBaseClient();
                if (DataBaseClientStatus)
                {
                    
                }//end if
                else
                {
                    /*if (VMuktiAPI.VMuktiInfo.CurrentPeer.SuperNodeIP == VMuktiAPI.VMuktiInfo.BootStrapIPs)
                    {
                        DataBaseClientStatus = true;
                    }
                    else
                    {
                    VMukti.Business.WCFServices.SuperNodeServices.DataContract.clsSuperNodeDataContract objSuperNodeDataContract = App.chHttpBootStrapService.svcHttpBsGetSuperNodeIP(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, GetIP4Address(), false);
                    VMuktiAPI.VMuktiInfo.CurrentPeer.SuperNodeIP = objSuperNodeDataContract.SuperNodeIP;
                    
                    //}
                }
            } //end while
        }*/


        public static string  GetIP4Address()
        {
            string IP4Address = String.Empty;
            try
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(System.Net.Dns.GetHostName()))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        IP4Address = IPA.ToString();
                        break;
                    }
                }
                return IP4Address;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "GetIP4Address--:--App.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return IP4Address;

            }
        }
        //end shilpa code
        //11-Feb-2008

        public static void Delete(int ID)
        {
            Delete(ID, null);
        }

        public static void Delete(int ID, IDbTransaction txn)
        {
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {                
                List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                objInfo.Direction = "Input";
                objInfo.PName = "@pID";
                objInfo.PValue = ID;
                objInfo.PDBType = "Int";
                objInfo.PSize = 200;

                lstSP.Add(objInfo);
                
                clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                CSqlInfo.objParam = lstSP;
                //shilpa code
                //14-Feb-2008
                try
                {

                VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteNonQuery("spDMyBuddy", CSqlInfo);
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    e.Data.Add("My Key", "Delete(int ID, IDbTransaction txn)--:--ClsBuddy.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteNonQuery("spDMyBuddy", CSqlInfo);
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    e.Data.Add("My Key", "Delete(int ID, IDbTransaction txn)--:--ClsBuddy.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteNonQuery("spDMyBuddy", CSqlInfo);
                }
                //end code
                //14-Feb-2008
            }
            else
            {
                new VMukti.DataAccess.ClsBuddyDataService(txn).Buddy_Delete(ID);
            }
        }

        public void Delete()
        {
            Delete(ID);
        }

        public void Delete(IDbTransaction txn)
        {
            Delete(ID, txn);
        }

        public void Save()
        {
            Save(null);
        }

        public void Save(IDbTransaction txn)
        {
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {            

                List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                objInfo.Direction = "InputOutput";
                objInfo.PName = "@pID";
                objInfo.PValue = _ID;                
                objInfo.PDBType = "Int";
                objInfo.PSize = 200;
                
                                
                clsSqlParametersInfo objInfo2 = new clsSqlParametersInfo();
                objInfo2.Direction = "Input";
                objInfo2.PName = "@pMyUserID";
                objInfo2.PValue = _MyUserID;                
                objInfo2.PDBType = "Int";
                objInfo2.PSize = 200;
                
                clsSqlParametersInfo objInfo3 = new clsSqlParametersInfo();
                objInfo3.Direction = "Input";
                objInfo3.PName = "@pUserID";
                objInfo3.PValue = _UserID;                
                objInfo3.PDBType = "Int";
                objInfo3.PSize = 200;

                
                lstSP.Add(objInfo);
                lstSP.Add(objInfo2);
                lstSP.Add(objInfo3);

                clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                CSqlInfo.objParam = lstSP;
                  try
                  {
                VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteNonQuery("spAEMyBuddy", CSqlInfo);
                  }
                  catch (System.ServiceModel.EndpointNotFoundException e)
                  {
                      e.Data.Add("My Key", "Save(IDbTransaction txn)--:--ClsBuddy.cs--:--" + e.Message + " :--:--");
                      ClsException.LogError(e);
                      ClsException.WriteToErrorLogFile(e);
                      VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                      VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteNonQuery("spAEMyBuddy", CSqlInfo);
                  }
                  catch (System.ServiceModel.CommunicationException e)
                  {
                      e.Data.Add("My Key", "Save(IDbTransaction txn)--:--ClsBuddy.cs--:--" + e.Message + " :--:--");
                      ClsException.LogError(e);
                      ClsException.WriteToErrorLogFile(e);
                      VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                      VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteNonQuery("spAEMyBuddy", CSqlInfo);
                  }
                
            }
            else
            {
                new VMukti.DataAccess.ClsBuddyDataService(txn).Buddy_Save(ref _ID, _MyUserID, _UserID);
            }
        }

        #endregion 
    }
}
